Skip to content

Conversation

@al-noori
Copy link
Contributor

@al-noori al-noori commented Jan 6, 2026

If a transform is applied via the GC, then both drawImage APIs consider now the best fitting handle by requesting it through the width/height of the full image scaled by the scaleFactor (relating destination width/height to source width/height) times the width/height scaling induced by the transformation.

Note that only the second commit is relevant for the PR. The first one references to the changes made in #2913.

To easily see and test, use the following snippet:

package org.eclipse.swt.snippets;

import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class Snippet389 {

public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell(display);
	shell.setText("Snippet 389");
	ImageFileNameProvider fileNameProvider = zoom -> {
		if (zoom == 200) {
			return "src/org/eclipse/swt/snippets/eclipse32.png";
		} else if (zoom == 100) {
			return "src/org/eclipse/swt/snippets/eclipse16.png";
		}
		return null;
	};
	Image image = new Image(display, fileNameProvider);
	shell.addPaintListener(e -> {
		e.gc.drawImage(image, 0, 0, 32, 32);
		e.gc.drawImage(image, 0, 0, 16, 16, 0, 50, 32, 32);
	});

	shell.addPaintListener(e -> {
		e.gc.setTransform(new Transform(display, 2, 0, 0, 2, 0, 0));
		e.gc.drawImage(image, 25, 0, 16, 16);
		e.gc.drawImage(image, 0, 0, 16, 16, 25, 25, 16, 16);
		e.gc.setTransform(null);
	});

	shell.open ();

	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
}

Below are screenshots of the snippet on a 100% zoom.

Before:
Before

After:
After

@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2026

Test Results (win32)

   34 files  ±0     34 suites  ±0   5m 1s ⏱️ +9s
4 636 tests ±0  4 563 ✅ ±0  73 💤 ±0  0 ❌ ±0 
  170 runs  ±0    167 ✅ ±0   3 💤 ±0  0 ❌ ±0 

Results for commit c4c66ad. ± Comparison against base commit 577b1d6.

♻️ This comment has been updated with latest results.

@arunjose696
Copy link
Contributor

Have tested the changes myself and can see it fixes the said issue.

@al-noori al-noori force-pushed the al-noori/FixDrawImageWithTransform branch 7 times, most recently from f9c3b63 to 35f2d85 Compare January 13, 2026 08:06
@akoch-yatta akoch-yatta force-pushed the al-noori/FixDrawImageWithTransform branch 2 times, most recently from ad4b117 to 3264f8b Compare January 16, 2026 08:17
@al-noori al-noori force-pushed the al-noori/FixDrawImageWithTransform branch from 3264f8b to be0c37d Compare January 16, 2026 09:40
@akoch-yatta akoch-yatta force-pushed the al-noori/FixDrawImageWithTransform branch from be0c37d to fca51c4 Compare January 19, 2026 16:03
Copy link
Contributor

@HeikoKlare HeikoKlare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an attempt to fix the issue with wrongly scaled image data being used when a transform is applied for GEF:

We should check if that issue is resolved with this PR as well and there is also a snippet in there to may better test these changes.

Comment on lines 1280 to 1281
float scaleWidth = (float) Math.hypot(m[0], m[2]);
float scaleHeight = (float) Math.hypot(m[1], m[3]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would help to add a comment with an explanation of a link to some explaining source why this calculates the right scale values out of the matrix.
It's not that easy to understand as the matrix of course also contains a rotations, so I have to admit I do not easily understand if/why this calculation is the right one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a short comment to it

@akoch-yatta akoch-yatta force-pushed the al-noori/FixDrawImageWithTransform branch from fca51c4 to 1cb3562 Compare January 20, 2026 08:36
If a transform is applied via the GC, then both drawImage
APIs consider now the best fitting handle by requesting it through the
width/height of the full image scaled by the scaleFactor (relating
destination width/height to source winowdth/height) times the width/height
scaling induced by the transformation.
@akoch-yatta akoch-yatta force-pushed the al-noori/FixDrawImageWithTransform branch from 1cb3562 to c4c66ad Compare January 21, 2026 11:33
Copy link
Contributor

@akoch-yatta akoch-yatta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I only rebased, moved a bit of code around and added a comment with my commits. Still I would ask for a second approval (@HeikoKlare)

I tested especially with Snippet389 and a slightly adjusted version to test with the eclipse16.svg to ensure is is really creating the perfect solution when putting in high scale factors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GC#drawImage() does not consider applied Transform when selecting best-fitting image source

4 participants